package fr.cephb.joperon.core.entities;

import javax.xml.bind.annotation.XmlTransient;

import com.sleepycat.db.DatabaseEntry;
import com.sleepycat.db.DatabaseException;

import fr.cephb.joperon.core.bio.Assembly;
import fr.cephb.joperon.core.entities.base.BasePrjFin;


/**
 * PrjFin
 * Description: Project logistics
 */
public class PrjFin
	extends BasePrjFin
	{
	public static enum Target
		{
		SNP { @Override
		public String getPrefix() {
			return "S";
			}},
		DNA { @Override
			public String getPrefix() {
				return "D";
			}
			}
		;
		public abstract String getPrefix();
		};
	
	/**
     *
     * Constructor
     *
     */
	public PrjFin()
		{
		super();
		}


        /**
         *
         * Copy Constructor
         *
         */
		public PrjFin(BasePrjFin cp)
			{
			super(cp);
			}
		
		/**
		 * constructor from a database entry
		 *
		 */
		public PrjFin(DatabaseEntry entry) throws DatabaseException
			{
			super(entry);
			}
		
		@Override
		public boolean equals(Object obj) {
			if(obj==this) return true;
			if(obj==null || !(obj instanceof PrjFin)) return false;
			PrjFin o=PrjFin.class.cast(obj);
			return o.getProjCode().equals(this.getProjCode())
				   ;
			}
		
		
		@Override
		public int hashCode()
			{
			return getProjCode().hashCode();
			}
		
		
        /**
         *
         * clone
         *
         */
		public Object clone()
			{
			return new PrjFin(this);
			}
		
		/** return the concatenation of pid+"_"+fix */
		public String getProjCode()
			{
			return getPid()+"_"+getFix();
			}
		
		public boolean hasTarget(Target t)
			{
			Target tgt= getTarget();
			if(tgt==null) return false;
			return tgt==t;
			}
		
		/** returns the related assembly or null */
		public Target getTarget()
			{
			String s= getFix();
			if(s==null) return null;
			for(Target t:Target.values())
				{
				if(s.startsWith(t.getPrefix())) return t;
				}
			return null;
			}
		
		/** set the assembly */
		public void setAssembly(Assembly as)
			{
			this.setOrg(as.getOrganism().getTreeLetterCode());
			this.setMapver(as.getVersion());
			this.setMapsrc(as.getSource());
			}

		/** returns the related assembly or null */
		@XmlTransient
		public Assembly getAssembly()
			{
			for(Assembly as: Assembly.getAssemblies())
				{
				if(
					getOrg()!=null && getOrg().equals(as.getOrganism().getTreeLetterCode()) &&
					getMapver()!=null && getMapver().equals(as.getVersion()) &&
					getMapsrc()!=null && getMapsrc().equals(as.getSource())
					)
					{
					return as;
					}
				}
			return null;
			}
		
	
		
		
	}



